package com.neuedu.util;

import com.auth0.jwt.JWT;
import com.auth0.jwt.algorithms.Algorithm;
import com.auth0.jwt.interfaces.DecodedJWT;
import com.neuedu.entity.Customer;

import java.security.interfaces.ECKey;
import java.security.interfaces.ECPublicKey;
import java.time.LocalDateTime;

public class JwtUtil {
    private static final String KEY = "wangyu";
    public static String getToken (Customer customer) {
        return JWT.create()
                .withClaim("phone", customer.getPhone())
                .withClaim("id", customer.getId())
                .withClaim("timestamp", System.currentTimeMillis())
                .sign(Algorithm.HMAC256(KEY));
    }
    public static Customer getCustomer(String token) {
        Customer customer = new Customer();
        DecodedJWT verify = JWT.require(Algorithm.HMAC256(KEY)).build().verify(token);
        customer.setId(verify.getClaim("id").asInt());
        customer.setPhone(verify.getClaim("phone").asString());
        return customer;
    }
}
